/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import businessObjects.Formulario;
import daos.FormularioDAO;
import daos.exceptions.NonexistentEntityException;
import daos.exceptions.PreexistingEntityException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author williamprado
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class FormularioServices implements FormularioServicesLocal {

    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    @PostConstruct
    public void initialize(){
    entityManager = entityManagerFactory.createEntityManager();
    }

    @PreDestroy
    public void cleanUp(){
        if(entityManager.isOpen()){
            entityManager.close();
        }
    }

    public void nuevoFormulario(Formulario form) {
        FormularioDAO fd = new FormularioDAO();
        try {
            fd.create(form, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(FormularioServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

     public Formulario consultarFormulario(int id) {
        FormularioDAO fd = new FormularioDAO();
        Formulario form = new Formulario();
        try {
            fd.findFormulario(id, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(FormularioServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return form;
    }

    public void actualizarFormulario(Formulario form) {
        FormularioDAO fd = new FormularioDAO();
        try {
            fd.edit(form, entityManager);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(FormularioServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(FormularioServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void eliminarFormulario(int id) {
        FormularioDAO fd = new FormularioDAO();
        try {
            fd.destroy(id, entityManager);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(FormularioServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(FormularioServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<Formulario> consultarFormulariosPauta(int idPauta) {
        try {
            FormularioDAO fd = new FormularioDAO();
            List<Formulario> result = null;
            List<Formulario> lf = fd.findFormularioEntities(entityManager);
            for (int i = 0; i < lf.size(); i++) {
                if (lf.get(i).getIdPauta().getIdPauta() == idPauta) {
                    result.add(lf.get(i));
                }
            }
            return result;
        }
        catch (Exception ex) {
            Logger.getLogger(FormularioServices.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method") 
}
